package overrideoverwrite;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName LambdaTest
 * @Date 2024/12/3 10:58 46
 * @Author Jack Chen
 * @Description serenity-cucumber-auto-project-jack
 * @Version 1.0
 */

interface MyInterface{
    public void doSomething(String s);
}


public class LambdaTest {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("apple", "ago", "xanana", "orange");

        //1、遍历输出

//        for (String s : list) {
//            System.out.println(s);
//        }
////        list.forEach(s->{
////            System.out.println(s);
////        });
//        list.forEach(System.out::println);

        //2、比较
//        Collections.sort(list, new Comparator<String>() {
//            @Override
//            public int compare(String o1, String o2) {
//                return o1.compareTo(o2);
//            }
//        });

//        Collections.sort(list, (o1, o2) -> o1.compareTo(o2));
//        list.forEach(System.out::println);

        //3、过滤
//        List<String> list2 = new ArrayList<>();
//        for (String s : list) {
//            if (s.startsWith("a")) {
//                list2.add(s);
//            }
//        }
//        list2.forEach(System.out::println);
//
//        List<String> list3 = list.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList());//Stream 转 List
//        list3.forEach(System.out::println);

        //映射
//        List<Integer> list4 = new ArrayList<>();
//        for (String s : list) {
//            list4.add(s.length());
//        }
//        System.out.println(list4);
//
//        List<Integer> list5 = list.stream().map(s -> s.length()).collect(Collectors.toList());//map的作用是转换
//        System.out.println(list5);

        //归约操作
//        List<Integer> list6 = Arrays.asList(1, 2, 3, 4, 5);
//        int sum = 0;
//        for (Integer v : list6) {
//            sum += v;
//        }
//        System.out.println(sum);
//
//        int sum2 = list6.stream().mapToInt(v -> v).sum();
//        int sum3 = list6.stream().reduce(0, (a, b) -> a + b); //identity代表初始值，0-1-3-6-
//        System.out.println(sum2);
//        System.out.println(sum3);

        //分组
//        Map<Integer, List<String>> groups = new HashMap<>();
//        for (String s : list) {
//            int length = s.length();
//            if(!groups.containsKey(length)) {
//                groups.put(length, new ArrayList<>());
//            }
//            groups.get(length).add(s); //按照字符串长度分组
//        }
//        System.out.println(groups);
//
//        Map<Integer, List<String>> groups2 =list.stream().collect(Collectors.groupingBy(s -> s.length()));
//        Map<Integer, List<String>> groups3 =list.stream().collect(Collectors.groupingBy(String::length));
//        System.out.println(groups2);
//        System.out.println(groups3);

        //函数式接口的实现
//        MyInterface myInterface = new MyInterface() {
//            @Override
//            public void doSomething(String s) {
//                System.out.println(s);
//            }
//        };
//        myInterface.doSomething("hello world");

//        MyInterface myInterface1 = (s) -> System.out.println(s);
//        MyInterface myInterface2 = System.out::println;
//        myInterface1.doSomething("hello world");
//        myInterface2.doSomething("hello jack chen");


        //创建线程
//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("hello world");
//            }
//        });
//        thread.start();

//        Thread thread1 = new Thread(()-> System.out.println("hello world1"));
//        thread1.start();

        //使用lambda表达式进行Option操作
//        String str = "hello world";
//        if (str != null) {
//            System.out.println(str.toUpperCase());
//        }

//        Optional.ofNullable(str).map(String::toUpperCase).ifPresent(System.out::println);


        //流水线操作
        List<String> list7 = new ArrayList<>();
        for (String s : list) {
            if (s.startsWith("a")) {
                list7.add(s.toUpperCase());
            }
        }
        Collections.sort(list7);
        System.out.println(list7);

        List<String> list8 = list.stream().filter(s -> s.startsWith("a"))
                .map(String::toUpperCase).sorted().collect(Collectors.toList());
        System.out.println(list8);

    }
}
